<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>创建对象</title>
</head>
<body>
    <script>
        var obj = new Object;

        obj.name = "Carl";
        obj.age = 34;

        obj.eat = function(){
            console.log("I'm eating.");
        };

        console.log(obj.name);
        console.log(obj.age);
        obj.eat();
        
        // Create object by factory model
        function createObject(name, age){
            var obj2 = new Object();
            obj2.name = name;
            obj2.age = age;

            obj2.sayHi = function(){
                console.log("My name is " + this.name + ". I'm " + this.age);
            };
            
            return obj2;

        };

        var per1 = createObject("Carl", 34);
        var per2 = createObject("Daniel", 5);

        per1.sayHi();
        per2.sayHi();

        // Structural function
        function Person(name, age){
            this.name = name;
            this.age = age;
            this.sayHi = function(){
                console.log("My name is " + this.name + ". I'm " + this.age + " years old.");
            };
        };

        var per3 = new Person("Carl", 34);
        per3.sayHi();
        console.log(per3 instanceof Person);

        // Method 3

        var per4 = {
            name: "Mechal",
            age: 40,
            sayHi: function(){
                console.log("My name is " + this.name + ". I'm " + this.age + " years old.");
            }
        };

        per4.sayHi();

    
    </script>
</body>
</html>